<?php

class KhenThuong extends Model
{
    public function selectByCanBo($cb_id)
    {
        $sql = 'SELECT cb_id, kt_id, kt_mota, DATE_FORMAT(kt_ngay, "%d/%m/%Y") AS kt_ngay FROM khenthuong WHERE cb_id = :id';
        return $this->_db->run($sql, array(':id' => $cb_id));
    }

    public function them($cb_id, $kt_mota, $kt_ngay)
    {
        $this->insert(array(
            'cb_id' => $cb_id,
            'kt_mota' => $kt_mota,
            'kt_ngay' => $kt_ngay
        ));

        return $this->_db->lastInsertId();
    }

    public function xoa($id)
    {
        return $this->delete('kt_id = :id', array('id' => $id));
    }

    public function sua($kt_id, $cb_id, $kt_mota, $kt_ngay)
    {
        return $this->update(array(
            'cb_id' => $cb_id,
            'kt_mota' => $kt_mota,
            'kt_ngay' => $kt_ngay
        ), 'kt_id = :id', array('id' => $kt_id));
    }

    public function selectById($id)
    {
        $rows = $this->select('kt_id = :id', array('id' => $id));
        if (count($rows) > 0) {
            $rows = $rows[0];
            $rows['kt_ngay'] = preg_replace('/^(\d{4})\-(\d{1,2})\-(\d{1,2})$/', '$3/$2/$1', $rows['kt_ngay']);
        }

        return $rows;
    }
}
